var request=require('request');
var cheerio=require('cheerio');
var path=require('path');
var fs=require('fs');
var requrl;
// var requrl='http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%BA%B7%E7%86%99+gif+%E6%90%9E%E7%AC%91&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%E5%BA%B7%E7%86%99+gif+%E6%90%9E%E7%AC%91&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn=60&rn=30&gsm=1e&1460699930633=';
for(var i=60;i<300;i=i+30){
	requrl='http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%BA%B7%E7%86%99%E6%9D%A5%E4%BA%86+gif&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%E5%BA%B7%E7%86%99%E6%9D%A5%E4%BA%86+gif&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn='+i+'&rn=30&gsm=1e&1460947600038=';
	// requrl='http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%BA%B7%E7%86%99+gif+%E6%90%9E%E7%AC%91&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=%E5%BA%B7%E7%86%99+gif+%E6%90%9E%E7%AC%91&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn='+i+'&rn=30&gsm=1e&1460699930633=';
	console.log(i);
	request(requrl,function (error,response,body) {
	// body...
	if(!error&&response.statusCode==200){
		// console.log(body);
		var obj=	JSON.parse(body);
// 		// console.log(obj);
		acquireData(obj);
	}else{
		console.log('似乎失败了');
	}
	})
}
// request(requrl,function (error,response,body) {
// 	// body...
// 	if(!error&&response.statusCode==200){
// 	     var obj=	JSON.parse(body);
// 		// console.log(obj);
// 		acquireData(obj);
// 	}else{
// 		console.log('似乎失败了');
// 	}
// 	})
function acquireData(obj){
	// var $=cheerio.load(data);
	// console.log($.html());
	// var meizi=$(".imgborder_bqimg").toArray();
	var datas=obj.data;
	console.log(datas);
	for(var el in datas ){
		var imgsrc=datas[el].thumbURL;
		var filename = parseUrlForFileName(imgsrc).replace("jpg","gif");
		// console.log(imgsrc,filename);
		if(imgsrc&&filename){
		downImg(imgsrc,filename,function(filename){
			// console.log(filename+'done');
		});	
		}
		
	}


}
function parseUrlForFileName(address){
	return path.basename(address);
}
function downImg(url,filename,callback){
	console.log(filename);
	var urll='http://img0.imgtn.bdimg.com/it/u=349127238,3719728242&fm=21&gp=0.jpg';
	var options={
		url:url,
		 headers: {
		 		Referer:'http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1460715132688_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%BA%B7%E7%86%99+gif+',
			cookie:'BDqhfp=%E5%BA%B7%E7%86%99%2Bgif%2B%E6%90%9E%E7%AC%91%26%260-10-1undefined%26%260%26%261; BDIMGISLOGIN=0; BDUSS=nBRenI2MFVCZWVOMmlCdHNmMlM2dVFPZzluWXZkT1lFT29OREk2bGx5MzhwV3RXQVFBQUFBJCQAAAAAAAAAAAEAAADVlkkLsK6yqb-owrMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPwYRFb8GERWb; PSTM=1449796007; BIDUPSID=E6A59873DA83D21E758D2E508B103652; BAIDUID=D55A4D18DB02AEC56D796D44C23B3CB9:FG=1; MCITY=-104%3A; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; H_PS_PSSID=1428_19672_18284_19690_17944_19558_15698_11978_10634; Hm_lvt_737dbb498415dd39d8abf5bc2404b290=1460435753,1460698804,1460714342,1460715098; Hm_lpvt_737dbb498415dd39d8abf5bc2404b290=1460715105'
	
		}
	}

	request.head(options,function(error,res,body){
		if(error){
			console.log('发生错误了'+'error');
		}
		console.log('现在爬取'+filename,body);
		request(options,function(error,res){
			console.log(res);
		}).pipe(fs.createWriteStream('baidu/'+filename)).on('error',function(e){
			
		}).on('close', callback);  
	})
}